//
//  CrystalMinds SQLiteDatabase Library
//  Version 2.3
//
//  Created by Mark van Wijnen on 16-10-09.
//  Last updated on 26-10-09.
//  Copyright 2009 CrystalMinds. All rights reserved.
//  Mail: info@crystalminds.nl
//
// This work is licensed under the Creative Commons Attribution-Noncommercial- 
// No Derivative Works 3.0 Unported License.
// To view this license go to http://creativecommons.org/licenses/by-nc-nd/3.0/ 
// or send a letter to Creative Commons, 171 Second Street, Suite 300, 
// San Francisco, California, 94105, VS.

#import "CMSQLiteDatabaseStatement.h"


@implementation CMSQLiteDatabaseStatement

@synthesize useCount;

- (sqlite3 *)database {
	// Return database handle
	return _databaseHandle;
}

- (void)dealloc {
	// Release memory
	if (_sqliteStatement != nil) {
		sqlite3_finalize(_sqliteStatement);
	}
	
	[super dealloc];
}

- (id)init {
	if (self = [super init]) {
		// Define use count
		useCount = 0;
	}
	return self;
}

- (id)initWithStatement:(sqlite3_stmt *)statement forDatabase:(sqlite3 *)database {
	if (self = [self init]) {
		// Set SQLite statement
		_sqliteStatement = statement;
		
		// Set database handle
		_databaseHandle = database;
	}
	return self;
}

- (void)reset {
	// Abort if no statement is set
	if (_sqliteStatement == nil) return;
	
	// Reset statement
	sqlite3_reset(_sqliteStatement);
}

- (NSString *)sql {
	// Abort if no statement is set
	if (_sqliteStatement == nil) return nil;
	
	// Define sqlite3 SQL
	const char *sqliteSQL = (const char *)sqlite3_sql(_sqliteStatement);
	
	// Define return value
	NSString *returnValue;
	
	if (sqliteSQL) {
		returnValue = [NSString stringWithUTF8String:sqliteSQL];
	} else {
		returnValue = @"";
	}

	// Return value
	return returnValue;	
}

- (void)used {
	useCount++;
}

- (sqlite3_stmt *)statement {
	// Return SQLite statement
	return _sqliteStatement;
}

@end
